package com.uchedao.seller.ui.car.base;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.baidu.mobstat.StatService;
import com.uchedao.seller.SellerApplication;
import com.uchedao.seller.http.VolleyUtil;
import com.uchedao.seller.manager.ImageLoadManager;
import com.uchedao.seller.util.LogUtil;

public abstract class BaseActivity extends FragmentActivity {

    /**
     * 页面标签
     */
    protected String TAG = "BaseActivity";
    /**
     *
     * */
    protected RequestQueue mQueue;

    /**
     * 设置页面标签
     */
    protected abstract String getActivityTag();

    /**
     * 获取布局XML ID
     */
    protected abstract int getLayoutResID();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化监听事件
     */
    protected abstract void initListener();

    protected void onSetting() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((SellerApplication)getApplication()).getActivityManager().pushActivity(this);
        onSetting();
        setContentView(getLayoutResID());
        //获取单例网络请求队列
        mQueue = VolleyUtil.getInstance().getRequestQueue();
        TAG = getActivityTag();
        init();
    }

    protected void init() {
        initView();
        initData();
        initListener();
        setDeBug();
    }

    /**
     * 设置日志系统
     */
    private void setDeBug() {
        LogUtil.isDebug = true;
    }

    @Override
    protected void onPause() {
        StatService.onPageEnd(this, TAG);
        //取消所有请求
        if (!TextUtils.isEmpty(TAG)) {
            mQueue.cancelAll(TAG);
        }
        super.onPause();
    }

    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, TAG);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((SellerApplication)getApplication()).getActivityManager().popActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ((SellerApplication)getApplication()).getActivityManager().popActivity(this);
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 添加网络请求
     */
    public void addQueue(Request request) {
        //设置标签
        request.setTag(TAG);
        if (mQueue != null) {
            mQueue.add(request);
        }
    }

    /**
     * 通过控件ID获取对应控件
     */
    public <T extends View>T getView(int viewId) {
        View view = findViewById(viewId);
        return (T)view;
    }

    public void setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
    }

    public void setImageByURL(int viewId, final String url) {
        ImageView iv = getView(viewId);
        ImageLoadManager.getInstance(this).displayImage(url, iv);
    }
}
